home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks '96 / Venus / Dialog.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  5.5 KB  |  146 lines  |  [TEXT/MPCC]

  1. /*
  2.  ***********************************************************************
  3.  *
  4.  *                            Generic Dialog Class
  5.  *
  6.  *            to handle a modeless dialog: a window with controls
  7.  *
  8.  * We call it a dialog because we want to take advantage of DialogManager
  9.  * functionality to figure out which items needs to be redrawn, etc. chores
  10.  *
  11.  * Note the dialog is derived from the ScreenWindow, and it considers
  12.  * ScreenWindow::this_window to be a DialogPtr
  13.  *
  14.  * Note, ModelessDialog::Item etc objects contain a dialog item handle, etc. information,
  15.  * which is only valid as long as the corresponding dialog is still there. Therefore,
  16.  * there is a danger of keeping and using an Item object after the dialog itself gets 
  17.  * disposed of. Declaring the Item class within the Modeless dialog _protected_ scope,
  18.  * although does not remove that danger, certainly decreases chances of that
  19.  * happening: this declaration makes sure that the Items are going to be used only
  20.  * within dialog member functions, the only place it makes sense.
  21.  *
  22.  ***********************************************************************
  23.  */
  24.  
  25. #pragma once
  26. #include "window.h"
  27. #include "mymenv.h"
  28.  
  29. class ModelessDialog : public ScreenWindow
  30. {
  31.   friend class BasicControl;
  32.   friend class UserItem;
  33.   
  34.   virtual void draw_user_item(const int item_no)         {}
  35.   virtual Boolean handle_item_hit(const int item_no) = 0;
  36.   virtual Boolean handle_activate(Boolean onoff); // Handle suspend/resume events
  37.   virtual void draw(void) {}                // DialogSelect does all the drawing for us
  38.  
  39.   virtual void destroy_it(void);            // Virtual destructor kludge
  40.  
  41.   Boolean generic_item_hit(const int item_no);     // Return FALSE if additional attention needed
  42. protected:
  43.                                             // Item control utilities
  44. //  void draw_default_item_border(const int item_no);
  45.  
  46.                                             // Generic dialog items and more specialized ones
  47.   class Item
  48.   {
  49.   protected:
  50.     Handle item_handle;
  51.     short  item_type;
  52.     Rect   item_rect;
  53.   public:
  54.     Item(const ModelessDialog& the_dialog, const int item_no);
  55.     operator const Rect * (void) const    { return &item_rect; }
  56.   };
  57.  
  58.   friend class ModelessDialog::Item;
  59.   
  60.   class ControlItem : public Item
  61.   {
  62.   public:
  63.     ControlItem(const ModelessDialog& the_dialog, const int item_no);
  64.     operator ControlHandle (void) const        { return (ControlHandle)item_handle; }
  65.     void set_title(const Str255 new_title)        { SetControlTitle((ControlHandle)item_handle,new_title); }
  66.   };
  67.  
  68.   class TextItem : public Item
  69.   {
  70.   public:
  71.     TextItem(const ModelessDialog& the_dialog, const int item_no);
  72.     void draw(const Str255 str);                // Draw a new text in an item
  73.   };
  74.  
  75.   class NumberItem : TextItem            // A slight variation of a TextItem to draw
  76.   {                                        // an int in ASCII
  77.   public:
  78.     NumberItem(const ModelessDialog& the_dialog, const int item_no);
  79.     void draw(const int number);                // Draw a new number in an item
  80.   };
  81.   
  82. public:
  83.                             // Create a color dialog from a resource template
  84.   ModelessDialog(const short resource_id);
  85.  
  86.                                   // ModelessDialog event dispatch entry points
  87.                                   // Return FALSE if this object doesn't want any
  88.                                   // more events
  89.   virtual Boolean handle_null_event(const long event_time);
  90.   virtual Boolean handle_event(const EventRecord& the_event);
  91. };
  92.  
  93.  
  94.                                         // Basic control object
  95.                                         // It's assumed that a pointer to this object
  96.                                         // is placed into RefCon of a control
  97.                                         // This object handles some specific
  98.                                         // click and highlighting features
  99.                                         // Thus it's a way of implementing custom
  100.                                         // control by slightly modifying a standard
  101.                                         // control, and all without messing with
  102.                                         // custom CDEF
  103. class BasicControl        
  104. {
  105.   friend class ModelessDialog;
  106.   const int signature;                        // To make sure we are who we are
  107.   enum { valid_sig = 0x43785 };
  108.   ControlHandle this_control;
  109.   
  110.  
  111.   static ControlDefProcPtr * real_defproc_handle;        // original ScrollBar CDEF handle
  112.   static ControlDefProcPtr * patch_defproc_handle;         // handle to our patch, see below
  113.   static ControlDefUPP defproc_patch_upp;                // Universal pointer to our patch
  114.   static pascal SInt32 defproc_patch                    // our patch itself
  115.       (SInt16 var_code, ControlRef the_control, ControlDefProcMessage message, SInt32 param);
  116.   //static pascal void track_action_relay(ControlHandle the_control, unsigned short part_no);
  117.   //static ControlActionUPP track_action_relay_upp;    // Universal pointer to the action_relay
  118.   virtual void track_action(const int part_no)         {}
  119.   
  120.                                               // Custom handle a mouse-click within this
  121.                                               // control. Return TRUE if it's _completely_
  122.                                               // handled, return FALSE if additional attention
  123.                                               // needed (say, control value was changed)
  124.   virtual Boolean handle_click(void)             { return FALSE;}
  125.   Boolean is_our_control(void) const             { return signature == valid_sig; }
  126. protected:
  127.   BasicControl(void) : signature(valid_sig), this_control(0) {}
  128.   ControlHandle our_control(void) const
  129.                           { assert( this_control != nil ); return this_control; }
  130.   void bind(const ModelessDialog::ControlItem& item);        // Late constructor
  131.   void set_value(const int new_value);        // Set a new value of a control, clipped to
  132.                                               // its [min,max] range
  133. };
  134.  
  135. class UserItem
  136. {
  137.   static pascal void user_item_relay_handler(WindowPtr the_dialog, short the_item);
  138.   static UserItemUPP user_item_relay_handler_upp;    // Universal pointer to the user_item_univ_handler
  139. protected:
  140.   Rect rect;
  141. public:
  142.   UserItem(void) {}
  143.   void bind(const ModelessDialog& the_dialog, const int item_no);        // Late constructor
  144.   void force_redraw(void)        { InvalRect(&rect); }
  145. };
  146.